package com.fingard.dsp.bank.directbank.wjrb01;

import com.fingard.dsp.bank.directbank.bankInterface.IBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.detail.RetAtsBatPayRecDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * @author: liuxb
 * @create: 2019-12-17 15:20
 * @version: 1.0
 **/
public class WJRB01BatPayRec extends WJRB01Base implements IBatPayRec {
    /**
     * 代发工资
     *
     * @param p_req1988 批量代付请求对象
     * @return 批量代付响应对象
     * @throws Exception
     */
    @Override
    public RetAtsBatPayRec d_1988_BatPay(ReqBatPayRec p_req1988) throws Exception {
        RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_req1988.reqHeader);
        retAts.writeStart();
        try {
            //======拼接报文======
            //拼接请求报文体
            WJRB01Header header = new WJRB01Header();
            header.TrsCode = "B2EAgentSalary";//银行交易码
            header.ERPTrsTimestamp = p_req1988.reqHeader.transTime;//指令请求时间
            header.ERPJnlNo = p_req1988.reqHeader.transSeqID;//生成交易流水号
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader(xmlWriter, header);
            xmlWriter.writeStartElement("Body");
            xmlWriter.writeElementString("PayerAcNo", p_req1988.ownAct);//付款账号
            xmlWriter.writeElementString("TotalCount", p_req1988.totalNum);//总笔数
            xmlWriter.writeElementString("Amount", p_req1988.totalAmt);//支付金额
            xmlWriter.writeElementString("GrantDate", p_req1988.payDate);//当前日期

            xmlWriter.writeStartElement("List");
            while (p_req1988.readRecord()) {
                xmlWriter.writeStartElement("Map");
                xmlWriter.writeElementString("PayeeAcNo", p_req1988.readedDetail.recAct);//对方账号
                xmlWriter.writeElementString("PayeeAcName", p_req1988.readedDetail.recName);//对方账户户名
                xmlWriter.writeElementString("PayeeAmount", p_req1988.readedDetail.payAmount);//金额
                xmlWriter.writeElementString("Remark", p_req1988.readedDetail.purpose);//用途
                xmlWriter.writeEndElement();
            }
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();

// 发送给银行获得应答报文
            String[] retStrArr = sendToBank(xmlWriter.toXmlString(), header);
            //模拟银行返回

//            String str = "<Message>\n" +
//                    "\t<Head>\n" +
//                    "\t\t<IBSReturnCode>000000</IBSReturnCode>\n" +
//                    "\t\t<IBSReturnMsg>成功！</IBSReturnMsg>\n" +
//                    "\t\t<IBSJnlNo>9126141</IBSJnlNo>\n" +
//                    "\t</Head>\n" +
//                    "\t<Body>\n" +
//                    "\t\t<ReturnCode>000000</ReturnCode>\n" +
//                    "\t\t<ReturnMsg>成功！</ReturnMsg>\n" +
//                    "<BatchNo>2233</BatchNo>\n" +
//                    "\t</Body>\n" +
//                    "</Message>\n";
//            String[] retStrArr = new String[]{"", str};

            if (retStrArr[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(retStrArr[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                    String tmpRetInfo = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                    header.ERPJnlNo = tmpBkReader.getFirstText("/Message/Head/IBSJnlNo");//交易流水号
                    if ("000000".equals(tmpRetCode)) {
                        String returnCode = tmpBkReader.getFirstText("/Message/Body/ReturnCode");
                        String returnMsg = tmpBkReader.getFirstText("/Message/Body/ReturnMsg");
                        retAts.respCode = "0";
                        retAts.reqSeqID = tmpBkReader.getFirstText("/Message/Body/BatchNo");
                        retAts.respInfo = returnMsg;
                        retAts.transState = returnCode;
                        retAts.payInfoCode = returnCode;
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpRetInfo);
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpSbMsg.toString());
                }
            } else {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(retStrArr[1].toString());
            }

        } catch (Exception e) {
            retAts.respCode = "-1";
            retAts.respInfo = e.toString();
            WriteBankLogLn("批量代发异常：" + e);
            e.printStackTrace();
        }
        retAts.writeRetToATS();
        return retAts;
    }

    /**
     * 批量代付状态查询
     *
     * @param p_req1998 批量代付查询请求对象
     * @return 批量代付响应对象
     * @throws Exception
     */
    @Override
    public RetAtsQryBatPayRec d_1998_QryBatPay(ReqQryBatPayRec p_req1998) throws Exception {
        RetAtsQryBatPayRec retAts = new RetAtsQryBatPayRec(p_req1998.reqHeader);
        retAts.writeStart();
        try {
            retAts.reqSeqID = p_req1998.reqBatchNO;
            WJRB01Header tmpBnkHeader = new WJRB01Header();
            tmpBnkHeader.TrsCode = "B2EAgentSalaryTrsQry";
            tmpBnkHeader.ERPTrsTimestamp = p_req1998.reqHeader.transTime;//指令请求时间
            tmpBnkHeader.ERPJnlNo = p_req1998.reqHeader.transSeqID;//生成交易流水号
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            writeCommonHeader2(xmlWriter, tmpBnkHeader);
            xmlWriter.writeStartElement("Body");
            xmlWriter.writeElementString("BatchNo", p_req1998.reqBatchNO);//批次号，跟ATS数据库字段保持一致
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();//写入报文完成

            // 发送给银行获得应答报文
            String[] resultBank = sendToBank(xmlWriter.toXmlString(), tmpBnkHeader);
            //模拟银行返回
//            String strm = "<Message>\n" +
//                    "\t<Head>\n" +
//                    "\t\t\t<IBSReturnCode>000000</IBSReturnCode>\n" +
//                    "\t\t\t<IBSReturnMsg>银企交易成功！</IBSReturnMsg>\n" +
//                    "\t</Head>\n" +
//                    "<Body>\n" +
//                    "\t\t<ReturnCode>000000</ReturnCode>\n" +
//                    "\t\t<ReturnMsg>成功！</ReturnMsg>\n" +
//                    "\t\t  <List>\n" +
//                    "\t\t\t\t<Map>\n" +
//                    "\t\t\t\t\t<PayeeAcNo>6235030010020007</PayeeAcNo>\n" +
//                    "\t\t\t\t\t<PayeeName>测试客户1</PayeeName>\n" +
//                    "<Amount>1.00</Amount>\n" +
//                    "\t\t\t\t\t<Remark>1111</Remark>\n" +
//                    "\t\t\t\t\t<Salary>0</Salary>\n" +
//                    "\t\t\t\t\t<FailReason>失败原因</FailReason>\n" +
//                    "\t\t\t\t</Map>\n" +
//                    "\t\t\t\t<Map>\n" +
//                    "\t\t\t\t\t<PayeeAcNo>6235030010020007</PayeeAcNo>\n" +
//                    "\t\t\t\t\t<PayeeName>测试客户2</PayeeName>\n" +
//                    "<Amount>1.20</Amount>\n" +
//                    "\t\t\t\t\t<Remark>2222</Remark>\n" +
//                    "\t\t\t\t\t<Salary>1</Salary>\n" +
//                    "\t\t\t\t\t<FailReason>失败原因</FailReason>\n" +
//                    "\t\t\t\t</Map>\n" +
//                    "\t\t</List>\n" +
//                    "\t</Body>\n" +
//                    "</Message>\n";
//            String[] resultBank = new String[]{"", strm};

            if (resultBank[0].length() == 0) {// 成功响应
                StringBuilder tmpSbMsg = new StringBuilder();//存提示信息
                XmlTextReader tmpBkReader = loadBankRespXmlReader(resultBank[1], tmpSbMsg);// 把银行返回报文转成XmlTextReader格式
                if (tmpBkReader != null) {// 返回报文的内容不为空
                    String IBSReturnCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                    String IBSReturnMsg = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
//                    retAts.reqSeqID = tmpBkReader.getFirstText("/Message/Head/IBSJnlNo");//银行流水号
                    if ("000000".equals(IBSReturnCode)) {
                        String returnCode = tmpBkReader.getFirstText("/Message/Body/ReturnCode");
                        String returnMsg = tmpBkReader.getFirstText("/Message/Body/ReturnMsg");
                        retAts.respCode = "0";
                        retAts.reqSeqID = p_req1998.reqBatchNO;
                        retAts.respInfo = returnMsg;
                        retAts.transState = returnCode;//处理代码
                        retAts.writeBeforeDetail();
                        while (tmpBkReader.read("/Message/Body/List/Map")) {
                            RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();
                            tmpRetDetail.recAct = tmpBkReader.readedXElement.getSingleTextNotNull("PayeeAcNo").trim();//收款账号
                            tmpRetDetail.recName = tmpBkReader.readedXElement.getSingleTextNotNull("PayeeName").trim();//收款账户户名
                            tmpRetDetail.payAmount = tmpBkReader.readedXElement.getSingleTextNotNull("Amount").trim();//收款金额
                            tmpRetDetail.memo = tmpBkReader.readedXElement.getSingleTextNotNull("Remark").trim();//备注
                            String msg = tmpBkReader.readedXElement.getSingleTextNotNull("Salary").trim();//结果状态

//                            tmpRetDetail.memo = tmpBkReader.readedXElement.getSingleTextNotNull("FailReason").trim();//处理信息

                            tmpRetDetail.result = msg;//处理代码
                            tmpRetDetail.payInfoCode = tmpRetDetail.result;//处理代码
                            tmpRetDetail.payAct = p_req1998.ownAct;//支付帐号
                            retAts.writeRetDetail(tmpRetDetail);
                        }
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(IBSReturnMsg);
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpSbMsg.toString());
                }
            } else {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(resultBank[1].toString());
            }
            retAts.writeEnd();
            return retAts;


        } catch (Exception e) {
            retAts.respCode = "-1";
            retAts.respInfo = e.getMessage();
            e.printStackTrace();
        }


        return null;
    }

    /**
     * 批量代扣提交
     *
     * @param p_req9188 批量代扣请求对象
     * @return 批量代扣响应对象
     * @throws Exception
     */
    @Override
    public RetAtsBatPayRec d_9188_BatPay(ReqBatPayRec p_req9188) throws Exception {
        return null;
    }

    /**
     * 批量代收状态查询
     *
     * @param p_req9198 批量代扣查询请求对象
     * @return 批量代扣查询响应对象
     * @throws Exception
     */
    @Override
    public RetAtsQryBatPayRec d_9198_QryBatPay(ReqQryBatPayRec p_req9198) throws Exception {
        return null;
    }
}

